class Solution:
    def isMagic(self, target) -> bool:
        n = len(target)
        index = 1
        flag = True
        c_oushu = n // 2
        k = 0
        while index <= n:
            if flag:
                if 2 * index == target[index - 1]:
                    k += 1
                    index += 1
                    if index > c_oushu:
                        flag = False
                else:
                    break
            else:
                if 2 * (index - c_oushu) - 1 == target[index - 1]:
                    k += 1
                    index += 1
                else:
                    break
        if k == 0:
            return False
        if k == n:
            return True
        index = 0
        s = [(i + 1) for i in range(n)]
        while s:
            s = s[1::2] + s[::2]
            t = s[:k]
            if t == target[index: index + len(t)]:
                index += len(t)
                s = s[k:]
            else:
                return False
        return True


data = Solution()
target = [2, 4, 3, 1, 5]
print(data.isMagic(target))